package com.wwtech.epp.ui.activity.work.presenter;

import android.support.annotation.NonNull;

import com.socks.library.KLog;
import com.wwtech.epp.data.api.HttpAPIWrapper;
import com.wwtech.epp.entity.BaseBack;
import com.wwtech.epp.entity.Comment;
import com.wwtech.epp.entity.ProjectDetail;
import com.wwtech.epp.entity.eventbus.Addstar;
import com.wwtech.epp.ui.activity.work.ProjectDetailActivity;
import com.wwtech.epp.ui.activity.work.contract.ProjectDetailContract;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.work
 * @Description: presenter of ProjectDetailActivity
 * @date 2017/09/15 15:02:18
 */
public class ProjectDetailPresenter implements ProjectDetailContract.ProjectDetailContractPresenter{

    HttpAPIWrapper httpAPIWrapper;
    private final ProjectDetailContract.View mView;
    private CompositeDisposable mCompositeDisposable;
    private ProjectDetailActivity mActivity;
    private ProjectDetail mProjectDetail;

    @Inject
    public ProjectDetailPresenter(@NonNull HttpAPIWrapper httpAPIWrapper, @NonNull ProjectDetailContract.View view, ProjectDetailActivity activity) {
        mView = view;
        this.httpAPIWrapper = httpAPIWrapper;
        mCompositeDisposable = new CompositeDisposable();
        this.mActivity = activity;
    }
    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        if (!mCompositeDisposable.isDisposed()) {
             mCompositeDisposable.dispose();
        }
    }

    @Override
    public void getProjectDetail(String projectId, Map map) {
        mView.showProgressDialog();
        Disposable disposable1 = httpAPIWrapper.getProjectDetail(projectId, map)
                .subscribe(new Consumer<ProjectDetail>() {
                    @Override
                    public void accept(ProjectDetail projectDetail) throws Exception {
                        //isSuccesse
                        mView.closeProgressDialog();
                        KLog.i("onSuccesse");
                        mView.showProject(projectDetail);
                        mProjectDetail = projectDetail;
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable1);
    }

    @Override
    public void addStar(Map map) {
        mView.showProgressDialog();
        if (mProjectDetail.getData().isStar()) {
            KLog.i("删除星标");
            Disposable disposable1 = httpAPIWrapper.deleteStar(map)
                    .subscribe(new Consumer<BaseBack>() {
                        @Override
                        public void accept(BaseBack baseBack) throws Exception {
                            //isSuccesse
                            mView.closeProgressDialog();
                            KLog.i("onSuccesse");
                            if (baseBack.getResultCode() == 0) {
                                if (mProjectDetail.getData().isStar()) {
                                    mProjectDetail.getData().setStar(false);
                                    EventBus.getDefault().post(new Addstar(mProjectDetail.getData().getProjectId(), false));
                                    mView.onAddStarBack(false);
                                } else {
                                    mProjectDetail.getData().setStar(true);
                                    EventBus.getDefault().post(new Addstar(mProjectDetail.getData().getProjectId(), true));
                                    mView.onAddStarBack(true);
                                }
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            //onError
                            KLog.i("onError");
                            mView.closeProgressDialog();
                            throwable.printStackTrace();
                        }
                    }, new Action() {
                        @Override
                        public void run() throws Exception {
                            //onComplete
                            KLog.i("onComplete");
                        }
                    });
            mCompositeDisposable.add(disposable1);
        } else {
            KLog.i("添加星标");
            Disposable disposable1 = httpAPIWrapper.addStar(map)
                    .subscribe(new Consumer<BaseBack>() {
                        @Override
                        public void accept(BaseBack baseBack) throws Exception {
                            //isSuccesse
                            mView.closeProgressDialog();
                            KLog.i("onSuccesse");
//                            ToastUtil.displayShortToast(baseBack.getResultMsg());
                            if (baseBack.getResultCode() == 0) {
                                if (mProjectDetail.getData().isStar()) {
                                    mProjectDetail.getData().setStar(false);
                                    EventBus.getDefault().post(new Addstar(mProjectDetail.getData().getProjectId(), false));
                                    mView.onAddStarBack(false);
                                } else {
                                    mProjectDetail.getData().setStar(true);
                                    EventBus.getDefault().post(new Addstar(mProjectDetail.getData().getProjectId(), true));
                                    mView.onAddStarBack(true);
                                }
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            //onError
                            KLog.i("onError");
                            mView.closeProgressDialog();
                            throwable.printStackTrace();
                        }
                    }, new Action() {
                        @Override
                        public void run() throws Exception {
                            //onComplete
                            KLog.i("onComplete");
                        }
                    });
            mCompositeDisposable.add(disposable1);
        }
    }

    @Override
    public void quickProject(Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.projectHurry(map)
                .subscribe(new Consumer<BaseBack>() {
                    @Override
                    public void accept(BaseBack baseBack) throws Exception {
                        //isSuccesse
                        mView.closeProgressDialog();
                        mView.onQuickSuccess();
                        KLog.i("onSuccesse");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void finishProject(Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.projectDone(map)
                .subscribe(new Consumer<BaseBack>() {
                    @Override
                    public void accept(BaseBack baseBack) throws Exception {
                        //isSuccesse
                        mView.closeProgressDialog();
                        mView.onFinishProjectSuccess();
                        KLog.i("onSuccesse");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void getProjectComment(String projectId, Map map) {
        Disposable disposable = httpAPIWrapper.getProjectComment(projectId, map)
                .subscribe(new Consumer<Comment>() {
                    @Override
                    public void accept(Comment comment) throws Exception {
                        //isSuccesse
                        mView.closeProgressDialog();
                        mView.showProjectComment(comment);
                        KLog.i("onSuccesse");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void deletProject(Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.deletProject(map)
                .subscribe(new Consumer<BaseBack>() {
                    @Override
                    public void accept(BaseBack baseBack) throws Exception {
                        //isSuccesse
                        mView.closeProgressDialog();
                        mView.deletSuccess();
                        KLog.i("onSuccesse");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void deletComment(String commentId, Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.deletComment(commentId, map)
                .subscribe(new Consumer<BaseBack>() {
                    @Override
                    public void accept(BaseBack baseBack) throws Exception {
                        //isSuccesse
                        mView.closeProgressDialog();
                        mView.deletCommentSuccess();
                        KLog.i("onSuccesse");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }
}